package zhh.tangbao.commodity.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import zhh.tangbao.commodity.dto.MachineQueryDto;
import zhh.tangbao.commodity.dto.MachineStockQueryDto;
import zhh.tangbao.commodity.entity.CommodityMachineEntity;
import zhh.tangbao.commodity.entity.CommodityMachineStock;
import zhh.tangbao.commodity.serivce.CommodityMachineService;
import zhh.tangbao.commodity.vo.MachineOptionsVo;
import zhh.tangbao.commodity.vo.StockDataVo;
import zhh.tangbao.system.annotation.Authority;
import zhh.tangbao.system.annotation.Log;
import zhh.tangbao.system.constant.AuthorityModel;
import zhh.tangbao.system.result.Result;

import java.util.List;

/**
 * <p>
 *     描述
 * </p>
 * @author 粥灰灰
 * <p>2023/3/2</p>
 */


@RestController
@RequestMapping("/api/machine")
public class CommodityMachineController {

    @Autowired
    private CommodityMachineService commodityMachineService;


    @PostMapping("/add")
    @Authority(authorityModel = AuthorityModel.CONTAIN, authorities = "machine")
    @Log(function = "添加机器信息", moduleName = "机器管理")
    public Result add(@RequestBody CommodityMachineEntity commodityMachineEntity) {
        return Result.successOrFail(commodityMachineService.add(commodityMachineEntity));
    }

    @DeleteMapping("/delete/{id}")
    @Authority(authorities = "machine", authorityModel = AuthorityModel.CONTAIN)
    @Log(function = "删除机器信息", moduleName = "机器管理")
    public Result delete(@PathVariable("id") String id) {
        return Result.successOrFail(commodityMachineService.delete(id));
    }

    @PostMapping("/update")
    @Authority(authorities = "machine", authorityModel = AuthorityModel.CONTAIN)
    @Log(function = "更新机器信息", moduleName = "机器管理")
    public Result update(@RequestBody CommodityMachineEntity commodityMachineEntity) {
        return Result.successOrFail(commodityMachineService.update(commodityMachineEntity));
    }

    @GetMapping("/get/{id}")
    @Authority(authorities = "machine", authorityModel = AuthorityModel.CONTAIN)
    public Result get(@PathVariable("id") String id) {
        return Result.success().put(commodityMachineService.get(id));
    }

    @GetMapping("/page")
    @Authority(login = false)
    public Result page(MachineQueryDto dto) {
        return Result.success().put(commodityMachineService.page(dto));
    }

    /**
     * 注意：此分页接口采用老式分页模式，所以页码需要从0开始算起，而使用Mybatis-plus分页插件的接口页码是从1开始算起
     * @param dto
     * @return
     */
    @GetMapping("/stockPage")
    @Authority(login = false)
    public Result stockPage(MachineQueryDto dto) {
        return Result.success().put(commodityMachineService.machineStockPage(dto));
    }

    @PostMapping("/addStock")
    @Authority(authorityModel = AuthorityModel.CONTAIN, authorities = "machine")
    @Log(function = "添加机器售卖商品", moduleName = "机器管理")
    public Result addStock(@RequestBody CommodityMachineStock commodityMachineStock) {
        return Result.successOrFail(commodityMachineService.addStock(commodityMachineStock));
    }

    @DeleteMapping("/deleteStock/{id}")
    @Authority(authorityModel = AuthorityModel.CONTAIN, authorities = "machine")
    @Log(function = "删除机器售卖商品", moduleName = "机器管理")
    public Result deleteStock(@PathVariable("id") Integer stockId) {
        return Result.successOrFail(commodityMachineService.deleteStock(stockId));
    }

    @PutMapping("/updateStock")
    @Authority(authorityModel = AuthorityModel.CONTAIN, authorities = "machine")
    @Log(function = "更新机器售卖商品", moduleName = "机器管理")
    public Result updateStock(@RequestBody CommodityMachineStock commodityMachineStock) {
        return Result.successOrFail(commodityMachineService.updateStock(commodityMachineStock));
    }

    @PutMapping("/getStock")
    @Authority(authorityModel = AuthorityModel.CONTAIN, authorities = "machine")
    public Result getStock(Integer stockId) {
        return Result.success().put(commodityMachineService.getStock(stockId));
    }

    @GetMapping("/getAllMachineStocks")
    @Authority(login = false)
    public Result getAllMachineStocks(MachineStockQueryDto dto) {
        return Result.success().put(commodityMachineService.getAllMachineStocks(dto));
    }

    @DeleteMapping("/deleteStocks")
    @Authority(authorityModel = AuthorityModel.CONTAIN ,authorities = "machine")
    @Log(function = "删除售货机库存内容", moduleName = "机器管理")
    public Result deleteStocks(@RequestBody List<Integer> ids) {
        return Result.successOrFail(commodityMachineService.deleteStocks(ids));
    }

    @PostMapping("/batchAddStock")
    @Authority(authorityModel = AuthorityModel.CONTAIN, authorities = "machine")
    @Log(function = "批量添加机器售卖商品", moduleName = "机器管理")
    public Result batchAddStock(@RequestBody List<CommodityMachineStock> commodityMachineStock) {
        return Result.successOrFail(commodityMachineService.batchAddStock(commodityMachineStock));
    }

    @PostMapping("/batchDelete")
    @Authority(authorityModel = AuthorityModel.CONTAIN, authorities = "machine")
    @Log(function = "批量删除机器", moduleName = "机器管理")
    public Result batchDelete(@RequestBody List<String> ids) {
        return Result.successOrFail(commodityMachineService.batchDelete(ids));
    }

    @GetMapping("/getStockSell")
    @Authority(login = false)
    public Result getStockSell(String machineId) {
        List<StockDataVo> result = commodityMachineService.getStockDataVo(machineId);
        return Result.success().put(result);
    }

    @GetMapping("/statePage")
    @Authority(login = false)
    public Result statePage(MachineQueryDto dto) {
        Page<CommodityMachineEntity> page = commodityMachineService.statePage(dto);
        return Result.success().put(page);
    }

    @GetMapping("/machineToOptions")
    @Authority(login = false)
    public Result machineToOptions() {
        List<MachineOptionsVo> vos = commodityMachineService.machineToOptions();
        return Result.success().put(vos);
    }
}
